package top.xtcoder.jdcbase.base.knife4j;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ConfigurationProperties(prefix="knife4j.global",
        ignoreInvalidFields=true,
        ignoreUnknownFields=true)
@Component
public class Knife4jPBGlobalRequestParam {
    private String test;
    private List<Knife4jPBRequestParameter> param;

    public Knife4jPBGlobalRequestParam() {
    }

    public Knife4jPBGlobalRequestParam(String test, List<Knife4jPBRequestParameter> param) {
        this.test = test;
        this.param = param;
    }

    /**
     * 全局参数
     * @return
     */
    public List<Parameter> getGlobalRequestParameters() {
        List<Parameter> parameters = new ArrayList<>();
        if(param == null) {
            return parameters;
        }
        param.forEach(param -> {
            parameters.add(new ParameterBuilder()
                    .name(param.getName())
                    .description(param.getDescription())
                    .modelRef(new ModelRef(param.getModelRef()))
                    .required(param.isRequired())
                    .parameterType(param.getParameterType())
                    .build());
        });
        return parameters;
    }

    //生成全局通用参数 v3.x
	/*private List<RequestParameter> getGlobalRequestParameters() {
	    List<RequestParameter> parameters = new ArrayList<>();
	    parameters.add(new RequestParameterBuilder()
	            .name("token")
	            .description("接口校验参数")
	            .required(false)
	            .in(ParameterType.HEADER)
	            .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
	            .required(false)
	            .build());
	     return parameters;
    }*/

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

    public List<Knife4jPBRequestParameter> getParam() {
        return param;
    }

    public void setParam(List<Knife4jPBRequestParameter> param) {
        this.param = param;
    }
}
